package com.yz.xyt.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;

import java.io.*;
import java.util.Map;

/**
 * 通用http发送方法
 */
public class HttpUtils
{
    /**
     * 发送post请求 json类型参数
     * @param param
     * @param url
     * @return
     */
    public static String sendPostRequest(String param,String url){
        HttpClientBuilder clientBuilder = HttpClientBuilder.create();
        HttpPost post  = new HttpPost(url);
        StringBuilder result = new StringBuilder();
        try {
            CloseableHttpClient closeableHttpClient = clientBuilder.build();
            HttpEntity entity =  new StringEntity(param,"UTF-8");
            post.setEntity(entity);
            post.setHeader("Content-type", "application/json");
            HttpResponse res = closeableHttpClient.execute(post);
            InputStream in = res.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in, "utf-8"));
            String line;
            while ((line = reader.readLine()) != null)
            {
                result.append(line);
            }
            reader.close();
            in.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return  result.toString();
    }

    /**
     * 发送get请求
     * @param url
     * @param params
     * @return
     */
    public static String sendGetRequest(String url, Map<String,String> params){
        //创建httpClient实例
        CloseableHttpClient client = HttpClients.createDefault();
        StringBuilder result = new StringBuilder();
        try {
            //创建一个uri对象
            URIBuilder uriBuilder = new URIBuilder(url);
            //塞入form参数
            for (Map.Entry<String,String> entry : params.entrySet()){
                uriBuilder.addParameter(entry.getKey(),entry.getValue());
            }
            //创建httpGet远程连接实例,这里传入目标的网络地址
            HttpGet httpGet = new HttpGet(uriBuilder.build());
            // 设置请求头信息，鉴权(没有可忽略)
            httpGet.setHeader("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
            // 设置配置请求参数(没有可忽略)
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 连接主机服务超时时间
                    .setConnectionRequestTimeout(35000)// 请求超时时间
                    .setSocketTimeout(60000)// 数据读取超时时间
                    .build();
            // 为httpGet实例设置配置
            httpGet.setConfig(requestConfig);
            //执行请求
            CloseableHttpResponse response = client.execute(httpGet);
            //获取响应实体, 响应内容
            InputStream in = response.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in, "utf-8"));
            String line;
            while ((line = reader.readLine()) != null)
            {
                result.append(line);
            }
            response.close();
            client.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return result.toString();
    }
}